package com.atguigu.cmsservice.controller;
import com.atguigu.cmsservice.entity.CrmBanner;
import com.atguigu.cmsservice.mapper.CrmBannerMapper;
import com.atguigu.cmsservice.service.CrmBannerService;
import com.atguigu.commonutils.Result;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/cmsservice/bannerfont")
@CrossOrigin
@EnableCaching //开启缓存的注解放在配置类和启动类的作用是一致的
public class CrmBannerController {

	@Resource
	private CrmBannerService bannerService;

	@ApiOperation(value = "获取Banner分页列表")
 	@GetMapping("{page}/{limit}")
	//value是缓存的名称，一个value下面可以有多个缓存的(keys)
    @Cacheable(key = "100",value = "pageList")
	public Result index(
			@ApiParam(name = "page", value = "当前页码", required = true)
			@PathVariable Long page,

			@ApiParam(name = "limit", value = "每页记录数", required = true)
			@PathVariable Long limit) {
		Page<CrmBanner> pageParam = new Page<>(page, limit);
		LambdaQueryWrapper<CrmBanner> lambdaQueryWrapper=new LambdaQueryWrapper<>();
		lambdaQueryWrapper.orderByDesc(CrmBanner::getId);
		bannerService.page(pageParam,null);
		return Result.ok().data("items", pageParam.getRecords()).data("total", pageParam.getTotal());
	}

	@ApiOperation(value = "获取Banner")
	@GetMapping("get/{id}")
	public Result get(@PathVariable String id) {
		CrmBanner banner = bannerService.getById(id);
		return Result.ok().data("item", banner);
	}

	@ApiOperation(value = "新增Banner")
	@PostMapping("save")
	public Result save(@RequestBody CrmBanner banner) {
		bannerService.save(banner);
		return Result.ok();
	}

	@ApiOperation(value = "修改Banner")
	@PutMapping("update")
	public Result updateById(@RequestBody CrmBanner banner) {
		bannerService.updateById(banner);
		return Result.ok();
	}

	@ApiOperation(value = "删除Banner")
	@DeleteMapping("remove/{id}")
	public Result remove(@PathVariable String id) {
		bannerService.removeById(id);
		return Result.ok();
	}
}

